/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Administrator
 */
public class WaitingRoomScene extends Scene implements IGameDisplay {

    private int gameRule;
    private int gamePlayType;
    private int playerId;
    private int tableHostId;
    private int numberPlayers;
    private int maxPlayer;
    private int numberPlayerReadys;
    private int tableId;
    private int roomId;
    private int myViewId;
    private int avatarWidth;
    private int avatarHeight;
    private boolean isSetupNumberPlayer;
    private boolean isKickOut;
    private boolean isSetupGameRule;
    private boolean isRequestFriend;
    private boolean isViewer;
//    private boolean isUpdate;
    //------------------------------------------------------------------------
    private int[] positionX;
    private int[] positionY;
    //-----------------------------------------------------------------------
    private String nameSend;
//    private String myName;
    private String gameRuleName;
    private String[] saveSubMenuString;
    private String[] subMenuString;
    //------------------------------------------------------------------------
    private Image tableHostImage;
    private Image bottomBarImage;
    private Image avatarImage;
    //-----------------------------------------------------------------------
    private CustomTextBox chatBox;
    private CustomTextBox moneyBetBox;
    private ShortShowBox[] messengerBox;
    private boolean isWaitingPass;
    private boolean isWaitSetUpNumberPlayers;
    private String[] listFriendNames;

    public WaitingRoomScene(MainCanvas parent) {
        super(parent);
        initValue();
        initObject();
    }

    private void initValue() {
        gameRule = -1;
        gameRuleName = "";
        questionStringInfo = " Bạn đã kết bạn thành công ";
        playerId = gameData.getPlayerId();
        tableHostId = gameData.getTableHostId();
        myViewId = gameData.getMyViewId();
        gameRule = gameData.getGameRule();
        gamePlayType = gameData.getGameTypePlay();
        switchGameRule(this.gameRule);
//        myName = gameData.getMyName();
        numberPlayerReadys = gameData.getNumberPlayerReadys();
        roomId = gameData.getRoomId();
        tableId = gameData.getTableId();
        numberPlayers = gameData.getNumberPlayers();
        maxPlayer = gameData.getMaxPlayer();
        setupSubMenu();
//        Debug.d("my money :" + gameData.getMyMoney());
        gameData.setGameDisplay(this);
    }

    private void initObject() {
        try {
            initImage();
            initPosition();
            initCustomBox();
            initMessengerBox();
        } catch (IOException ex) {
        }
    }

    private void initCustomBox() {
        moneyBetBox = new CustomTextBox(PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, false, keyPad);
        moneyBetBox.setTypeInput(2);
        moneyBetBox.setShowTypeInput(false);
        chatBox = library.getChatBox(keyPad);
    }

    private void initMessengerBox() {
        messengerBox = library.getMessengerBox();
    }

    private void initPosition() {
        avatarWidth = avatarImage.getWidth() / 11;
        avatarHeight = avatarImage.getHeight() / 2;
        positionX = new int[4];
        positionX[0] = positionX[2] = canvasWidth / 2 - avatarWidth / 2;
        positionX[3] = leftScreen + 10;
        positionX[1] = canvasWidth - 10 - avatarWidth;
        positionY = new int[4];
        positionY[0] = canvasHeight - avatarHeight - 10 - bottomBarImage.getHeight();
        positionY[1] = positionY[3] = canvasHeight / 2 - avatarHeight / 2 - bottomBarImage.getHeight();
        positionY[2] = topScreen + 10;
    }

    private void initImage() throws IOException {
        tableHostImage = Utils.loadImage("table_host.png");
        bottomBarImage = library.getBottomBarImage();
        avatarImage = Utils.loadImage("new_avatar.png");
    }

    private void setupSubMenu() {
        //tableHostId = 1;
        if (subMenu == null) {
            subMenu = new MenuObject();
        }
        switch (gamePlayType) {
            case Constants.BA_CAY:
                if (playerId == tableHostId) {
                    subMenuString = new String[]{"Nạp tiền", "Kết bạn", "Loa", "Kick", "Thiết lập tiền cược", "Thiết lập số người", "Đặt mật khẩu"};
                } else {
                    subMenuString = new String[]{"Nạp tiền", "Kết bạn", "Loa"};
                }
                break;
            case Constants.TIEN_LEN:
                if (playerId == tableHostId) {
                    subMenuString = new String[]{"Nạp tiền", "Kết bạn", "Loa", "Kick", "Thiết lập tiền cược", "Thiết lập số người", "Đặt mật khẩu"};
                } else {
                    subMenuString = new String[]{"Nạp tiền", "Kết bạn", "Loa"};
                }
                break;
            case Constants.PHOM:
                if (playerId == tableHostId) {
                    subMenuString = new String[]{"Nạp tiền", "Kết bạn", "Loa", "Kick", "Thiết lập tiền cược", "Thiết lập số người", "Đăt mật khẩu", "Thiết lập kiểu chơi"};
                } else {
                    subMenuString = new String[]{"Nạp tiền", "Kết bạn", "Loa"};
                }
                break;
            case Constants.CO_VUA:
                if (playerId == tableHostId) {
                    subMenuString = new String[]{"Nạp tiền", "Kết bạn", "Loa", "Kick", "Thiết lập tiền cược", "Đặt mật khẩu"};
                } else {
                    subMenuString = new String[]{"Nạp tiền", "Kết bạn", "Loa"};
                }
                break;
        }
        subMenu.setList(subMenuString);
        if (parent != null) {
            parent.repaint();
        }
    }
    //----------------------------------------------------------------------------

    public void setListSubMenu() {
    }
//-----------------------------------------------------------------------

    public void doLoop() {
        super.doLoop();
        if (isShowBetBox && countFrame % 4 == 0) {
            moneyBetBox.run();
            parent.repaint();
            return;
        }
        if (isShowChatBox && countFrame % 4 == 0) {
            chatBox.run();
            parent.repaint();
            return;
        }

        for (int i = 0; i < 4; i++) {
            if (messengerBox[i].isShow) {
                messengerBox[i].run();
                parent.repaint();
            }
        }
        if (dialog != null) {
            if (dialog.isShow) {
                dialog.run();
                parent.repaint();
            }
        }

    }
//-------------------------------------------------------------------------

    public void paintComponents(Graphics g) {
        paintPlayerInfo(g);
        paintBarMenu(g);
        paintTableInfo(g);
        if (isSetupNumberPlayer || isSetupGameRule || isKickOut || isRequestFriend) {
            paintSubMenu(g);
        }
        if (isShowBetBox) {
            paintBetBox(g);
        }
        paintMessageChat(g);
        if (isShowChatBox) {
            chatBox.paint(g, leftScreen + 10, canvasHeight - 45, canvasWidth - 20, 20);
        }
        if (dialog != null) {
            dialog.paintInput(g, leftScreen + 10, canvasHeight - 5, canvasWidth - 20);
        }
    }

    private void paintTableInfo(Graphics g) {
        //  g.drawImage(playIconImage, canvasWidth / 2 - playIconImage.getWidth() / 2, canvasHeight / 2 - playIconImage.getHeight() / 2 - bottomBarImage.getHeight(), 0);
        int top = canvasHeight / 2 - 50;
        PiPoDesigner.drawCenterString(g, "Tiền cược :" + gameData.getMoneyBet(), PiPoDesigner.COLOR_BLACK,
                PiPoDesigner.COLOR_YELOW, canvasWidth / 2, top);
        PiPoDesigner.drawCenterString(g, "Phòng :" + (roomId + 1) + " Bàn :" + (tableId + 1), PiPoDesigner.COLOR_BLACK,
                PiPoDesigner.COLOR_RED, canvasWidth / 2, top + 17);

        if (gameRuleName.length() > 0) {
            PiPoDesigner.drawString(g, gameRuleName, PiPoDesigner.COLOR_BLACK,
                    PiPoDesigner.COLOR_GREEN, canvasWidth / 2 - PiPoDesigner.stringWidth(gameRuleName) / 2, top + 34);
        }
    }

    private void paintBetBox(Graphics g) {
        int betDialogWidth = 100;
        int betDialogHeight = 20;
        int betDiaglogX = canvasWidth / 2 - betDialogWidth / 2;
        int betDialogY = canvasHeight - betDialogHeight - 30;
        moneyBetBox.paint(g, betDiaglogX, betDialogY, betDialogWidth, betDialogHeight);
    }

    private void paintMessageChat(Graphics g) {
        messengerBox[0].paint(g);
        messengerBox[1].paint(g);
        messengerBox[2].paint(g);
        messengerBox[3].paint(g);
    }

    protected void paintSubMenu(Graphics g) {
        subMenu.paint(g, leftScreen, canvasHeight - bottomBarHeight);
    }

    protected void paintBarMenu(Graphics g) {
        g.drawImage(bottomBarImage, leftScreen, canvasHeight - bottomBarImage.getHeight(), 0);
//        if (isShowSpeaker) {
//            PiPoDesigner.drawString(g, "Đóng", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 5, canvasHeight - 20);
//            PiPoDesigner.drawString(g, "Xóa", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 30, canvasHeight - 20);
//            PiPoDesigner.drawString(g, "Gửi", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - 20, canvasHeight - 20);
//            return;
//        }
        if (isShowChatBox || isShowSpeaker || isShowBetBox) {
            PiPoDesigner.drawString(g, "Đóng", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 5, canvasHeight - 25);
            PiPoDesigner.drawString(g, "Xóa", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 30, canvasHeight - 25);
            PiPoDesigner.drawString(g, "Gửi", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - 20, canvasHeight - 25);
            return;
        } else {
            PiPoDesigner.drawString(g, "Menu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 5, canvasHeight - 25);
            if (!isShowSubMenu && !isSetupGameRule && !isKickOut && !isSetupNumberPlayer && !isRequestFriend) {
                if (gameData.readyStates[playerId] == 0 || playerId == tableHostId) {
                    PiPoDesigner.drawString(g, "Thoát", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 40, canvasHeight - 25);
                }
            } else {
                PiPoDesigner.drawString(g, "Đóng", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 40, canvasHeight - 25);
            }
        }

        if (!isShowBetBox) {
            if (playerId == tableHostId) {
                PiPoDesigner.drawString(g, "Bắt đầu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - 25, canvasHeight - 25);
            } else {
                if (gameData.getMyReadyState() == 0) {
                    PiPoDesigner.drawString(g, "Sẵn sàng", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - 25, canvasHeight - 25);
                } else {
                    PiPoDesigner.drawString(g, "Bỏ sẵn sàng", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - 25, canvasHeight - 25);
                }
            }
        } else {
            PiPoDesigner.drawString(g, "Đặt cược", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - 25, canvasHeight - 25);
        }
    }

    private void paintPlayerInfo(Graphics g) {
        for (int i = 0; i < maxPlayer; i++) {
            if (!gameData.isPlays[i]) {
                continue;
            }
            int positionId = (i - playerId + maxPlayer) % maxPlayer;
            int nameWidth = PiPoDesigner.stringWidth(gameData.playerName[i]);

            Utils.drawFrame(g, avatarImage, positionX[positionId], positionY[positionId], gameData.avatarIds[i], gameData.playerGender[i], avatarWidth, avatarHeight);
            switch (positionId) {
                case 0:
                    if (nameWidth < 50) {
                        paitnInfo(g, gameData.playerName[i], gameData.playerMoneys[i], positionX[positionId] + avatarWidth, positionY[positionId] + 12);
                    } else {
                        g.setClip(positionX[positionId] + avatarWidth, positionY[positionId] + 12, 50, 30);
                        paitnInfo(g, gameData.playerName[i], gameData.playerMoneys[i], positionX[positionId] + avatarWidth, positionY[positionId] + 12);
                        g.setClip(0, 0, canvasWidth, canvasHeight);
                        PiPoDesigner.drawString(g, "...", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, positionX[positionId] + avatarWidth + 50, positionY[positionId] + 12);
                    }
                    paintReadyState(g, i, gameData.readyStates[i], positionX[positionId] + 10, positionY[positionId] - 12);
                    break;
                case 1:
                    paintReadyState(g, i, gameData.readyStates[i], positionX[positionId] + 10, positionY[positionId] - 12);
                    if (nameWidth < 50) {
                        paitnInfo(g, gameData.playerName[i], gameData.playerMoneys[i], canvasWidth - PiPoDesigner.stringWidth(gameData.playerName[i]) - 5, positionY[positionId] + avatarHeight - 10);
                    } else {
                        paitnInfo(g, gameData.playerName[i], gameData.playerMoneys[i], canvasWidth - 50, positionY[positionId] + avatarHeight - 10);
                    }
                    break;
                case 2:
                    paintReadyState(g, i, gameData.readyStates[i], positionX[positionId], positionY[positionId] - 10);
                    if (nameWidth < 50) {
                        paitnInfo(g, gameData.playerName[i], gameData.playerMoneys[i], positionX[positionId] + avatarWidth, positionY[positionId] + 12);
                    } else {
                        g.setClip(positionX[positionId] + avatarWidth, positionY[positionId] + 12, 50, 30);
                        paitnInfo(g, gameData.playerName[i], gameData.playerMoneys[i], positionX[positionId] + avatarWidth, positionY[positionId] + 12);
                        g.setClip(0, 0, canvasWidth, canvasHeight);
                        PiPoDesigner.drawString(g, "...", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, positionX[positionId] + avatarWidth + 50, positionY[positionId] + 12);
                    }
                    break;
                case 3:
                    paintReadyState(g, i, gameData.readyStates[i], positionX[positionId] + 5, positionY[positionId] - 12);
                    if (nameWidth < 50) {
                        paitnInfo(g, gameData.playerName[i], gameData.playerMoneys[i], positionX[positionId], positionY[positionId] + avatarHeight - 10);
                    } else {
                        g.setClip(positionX[positionId], positionY[positionId] + avatarHeight - 10, 50, 30);
                        paitnInfo(g, gameData.playerName[i], gameData.playerMoneys[i], positionX[positionId], positionY[positionId] + avatarHeight - 10);
                        g.setClip(0, 0, canvasWidth, canvasHeight);
                        PiPoDesigner.drawString(g, "...", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, positionX[positionId] + 50, positionY[positionId] + avatarHeight - 10);
                    }
                    break;
            }
        }

    }

    private void paintReadyState(Graphics g, int id, int state, int x, int y) {
        if (Debug.isTest) {
            int posId = (id - playerId + maxPlayer) % maxPlayer;
            switch (posId) {
                case 0:
                    g.drawImage(tableHostImage, x - 7, y + 8, 0);
                    break;
                case 1:
                    g.drawImage(tableHostImage, x - 5, y + 8, 0);
                    break;
                case 2:
                    g.drawImage(tableHostImage, x + 5, y + 5, 0);
                    break;
                case 3:
                    g.drawImage(tableHostImage, x, y + 8, 0);
                    break;
            }
            return;
        }
        if (id == tableHostId) {
            int posId = (id - playerId + maxPlayer) % maxPlayer;
            switch (posId) {
                case 0:
                    g.drawImage(tableHostImage, x - 7, y + 8, 0);
                    break;
                case 1:
                    g.drawImage(tableHostImage, x - 5, y + 8, 0);
                    break;
                case 2:
                    g.drawImage(tableHostImage, x + 5, y + 5, 0);
                    break;
                case 3:
                    g.drawImage(tableHostImage, x, y + 8, 0);
                    break;
            }
            return;
        } else {
            if (state == 1) {
                PiPoDesigner.drawCenterString(g, "OK", PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_YELOW, x + 5, y);
            }

        }
    }

    private void paitnInfo(Graphics g, String name, long money, int x, int y) {
        // name
        PiPoDesigner.drawString(g, name, PiPoDesigner.COLOR_BLACK,
                PiPoDesigner.COLOR_WHITE, x, y);
        //money
        PiPoDesigner.drawString(g, String.valueOf(money), PiPoDesigner.COLOR_BLACK,
                PiPoDesigner.COLOR_PINK, x, y + 13);
    }
//--------------------------------------------------------------------------

    public void doKeyLeft() {
        if (isShowTableSms && screenMode == SCREEN_LANDSCAPE) {
            charMoneyIndex = (charMoneyIndex < 3 ? 3 : 0);
            parent.repaint();
            return;
        }
    }

    public void doKeyRight() {
        if (isShowTableSms && screenMode == SCREEN_LANDSCAPE) {
            charMoneyIndex = (charMoneyIndex < 3 ? 3 : 0);
            parent.repaint();
            return;
        }
    }

    protected void doKeyUpRemain() {
        if (isRequestFriend || isSetupNumberPlayer || isSetupGameRule || isKickOut) {
            subMenu.doKeyUp();
            parent.repaint();
            return;
        }
    }

    protected void doKeyDownRemain() {
        if (isSetupNumberPlayer || isSetupGameRule || isKickOut || isRequestFriend) {
            subMenu.doKeyDown();
            parent.repaint();
            return;
        }
    }

    protected void doSoftKeyLeftRemain() {
        if (isShowChatBox) {
            Debug.d("close chat box");
            isShowChatBox = false;
            chatBox.setText("");
            parent.repaint();
            return;
        }
        if (isShowBetBox) {
            isShowBetBox = false;
            moneyBetBox.setText("");
            parent.repaint();
            return;
        }
    }

    protected void doSoftKeyRightRemain() {
        if (dialog != null) {
            if (dialog.isShow) {
                dialog.doSoftKeyRight();
                parent.repaint();
                return;
            }
        }
        if (isShowChatBox) {
            chatBox.doSoftKeyRight();
            parent.repaint();
            return;
        }
        if (isShowBetBox) {
            moneyBetBox.doSoftKeyRight();
            parent.repaint();
            return;
        }
        if (isSetupGameRule || isSetupNumberPlayer || isRequestFriend || isKickOut) {
            changeSubMenu(saveSubMenuString);
            isSetupNumberPlayer = false;
            isSetupGameRule = false;
            isRequestFriend = false;
            isKickOut = false;
            parent.repaint();
            return;
        }
        if (gameData.readyStates[playerId] == 0 || playerId == tableHostId) {
            Request.exitTable();
            isLoading = true;
            parent.repaint();
        }
    }

    protected void doKeyCenterRemain() {
        if (isLoading) {
            return;
        }
        if (dialog != null) {
            if (dialog.isShow && issuerName.length() == 0) {
                if (dialog.getText().length() == 0) {
                    return;
                }
                if (dialog.getText().length() < 6) {
                    Request.setTablePass(dialog.getText());
                    isLoading = true;
                    isWaitingPass = true;
                } else {
                    dialogString = "Mật khẩu tối đi 5 ký tự";
                }

                dialog.show(false);
                parent.repaint();
                return;
            }
        }

        if (isShowChatBox && !isViewer) {
            String messageText = chatBox.getText();
            if (messageText.length() == 0) {
                isShowChatBox = false;
                parent.repaint();
                return;
            }
            if (messageText.length() > 25) {
                dialogString = "Số kí tự chat không vượt quá 25";
                parent.repaint();
                return;
            }
            switchMessengerBox(0, messageText);
            isShowChatBox = false;
            Request.chat(chatBox.getText());
            chatBox.setText("");
            parent.repaint();
            return;
        }
        if (isRequestFriend) {
            doFriendRequest();
            return;
        }
        if (isSetupGameRule) {
            doSetupGameRule();
            return;
        }
        if (isSetupNumberPlayer) {
            doSetupNumberPlayer();
            return;
        }

        if (isKickOut) {
            doKickOut();
            return;
        }
        if (isShowBetBox) {
            isShowBetBox = false;
            setMoneyBet();
            return;
        }
        changeReadyState();
    }

    private void changeReadyState() {
        int state = gameData.getMyReadyState();
//        Debug.d("number playre :" + numberPlayers);
//        Debug.d("ready :" + numberPlayerReadys);
        if (playerId == tableHostId) {
            if (numberPlayerReadys == numberPlayers - 1) {
                if (numberPlayers == 1) {
                    dialogString = "Chưa có người chơi";
                    parent.repaint();
                } else {
                    if (gameData.getMyMoney() > Integer.parseInt(gameData.getMoneyBet()) * 5) {
                        isLoading = true;
                        gameData.setMyReadyState(1 - state);
                        Request.changeReadyState(1 - state);
                        parent.repaint();
                    } else {
                        isLoading = true;
                        gameData.isOutOfMoney = true;
                        Request.exitTable();
                        parent.repaint();
                    }
                }
            } else {
                dialogString = "Người chơi chưa sẵn sàng";
                parent.repaint();
            }
        } else {
            if (Integer.parseInt(gameData.getMoneyBet()) <= gameData.getMyMoney()) {
                isLoading = true;
                gameData.readyStates[playerId] = 1 - state;
                Request.changeReadyState(1 - state);
                parent.repaint();
            } else {
                dialogString = "Bạn không đủ tiền để chơi";
                parent.repaint();
            }
        }
        return;
    }

    private void setMoneyBet() {
        String newMoneyBet = moneyBetBox.getText();
        if (newMoneyBet.length() > 0) {
            if (Integer.parseInt(newMoneyBet) * 5 > gameData.getMyMoney()) {
                dialogString = "Không đặt quá 1/5 lần số tiền hiện có";
                newMoneyBet = "0";
            } else {
                Request.betMoney(newMoneyBet);
                isLoading = true;
            }
        } else {
            newMoneyBet = "0";
        }
        parent.repaint();
    }

    public void inputKeyCode(int key) {
        if (dialog != null) {
            if (dialog.isShow) {
                if (issuerName.length() == 0) {
                    if (dialog.getText().length() <= 5) {
                        dialog.input(key);
                    } else {
                        dialogString = "Mật khẩu không quá 5 ký tự";
                    }
                } else {
                    dialog.input(key);
                }
                parent.repaint();
                return;
            }
        }
        if (isShowBetBox) {
            if (moneyBetBox.getText().length() >= 10) {
                dialogString = "Số tiền bạn nhập vào quá lớn.";
                parent.repaint();
                return;
            }
            moneyBetBox.inputChar(key);
            parent.repaint();
            return;
        }
        if (isShowSpeaker) {
            inputSpeaker(key);
            return;
        }
        if (chatBox.getText().length() <= 25) {
            chatBox.inputChar(key);
        }
        isShowChatBox = true;
        parent.repaint();
    }

    private void doKickOut() {
        Request.kickOut(listFriendNames[subMenu.getSelectedIndex()]);
        isKickOut = false;
        changeSubMenu(saveSubMenuString);
        parent.repaint();
    }

    private void doSetupNumberPlayer() {
        int number = 0;
        switch (subMenu.getSelectedIndex()) {
            case 0:
                number = 4;
                break;
            case 1:
                number = 3;
                break;
            case 2:
                number = 2;
                break;
        }
        if (number >= numberPlayers) {
            Request.setupNumberPlayers(number);
            isLoading = true;
            isWaitSetUpNumberPlayers = true;
        } else {
            dialogString = "Bạn thiết lập số người chơi ít hơn số người hiện tại.";
        }
        isSetupNumberPlayer = false;
        changeSubMenu(saveSubMenuString);
        parent.repaint();
    }

    private void switchMessengerBox(int posId, String text) {
        switch (posId) {
            case 0:
                messengerBox[0].setText(text);
                messengerBox[0].setPosition(canvasWidth / 2 - messengerBox[0].getBoxWidth() / 2, positionY[0]);
                break;
            case 1:
                messengerBox[1].setText(text);
                messengerBox[1].setPosition(canvasWidth - 10 - messengerBox[1].getBoxWidth(), positionY[1]);
                break;
            case 2:
                messengerBox[2].setText(text);
                messengerBox[2].setPosition(canvasWidth / 2 - messengerBox[2].getBoxWidth() / 2, positionY[2]);
                break;
            case 3:
                messengerBox[3].setText(text);
                messengerBox[3].setPosition(positionX[3], positionY[3]);
                break;
        }
        messengerBox[posId].show(3000);
    }

    private void doSetupGameRule() {
        Request.changeGameRule(subMenu.getSelectedIndex());
        switchGameRule(subMenu.getSelectedIndex());
        isSetupGameRule = false;
        changeSubMenu(saveSubMenuString);
        parent.repaint();
    }

    private void switchGameRule(int index) {

        switch (gamePlayType) {
            case Constants.BA_CAY:
                if (index == 0) {
                    gameRule = Constants.BA_CAY_NHAT;
                    gameRuleName = "Luật chơi: nhất ăn tất";
                } else if (index == 1) {
                    gameRule = Constants.BA_CAY_CHUONG;
                    gameRuleName = "Luật chơi: chương";
                }

                break;
            case Constants.PHOM:
                if (index == 0) {
                    gameRule = Constants.PHOM_THUONG;
                    gameRuleName = "Luật chơi: không ký gửi";
                } else if (index == 1) {
                    gameRule = Constants.PHOM_GUI;
                    gameRuleName = "Luật chơi: có ký gửi";
                }
                break;
            default: {
                gameRule = 1;
                gameRuleName = "";
            }
        }
        parent.repaint();
    }

    private void doFriendRequest() {
        int index = subMenu.getSelectedIndex();
        String name = subMenuString[index];

        isRequestFriend = false;
        dialogString = "Yêu cầu kết bạn đã được gửi";
        Request.sendFriendRequest(gameData.myName, name);

        changeSubMenu(saveSubMenuString);
        parent.repaint();
    }

    private void changeSubMenu(String[] newSubMenuString) {
        saveSubMenuString = subMenuString;
        subMenuString = newSubMenuString;
        subMenu.setList(subMenuString);
    }

//------------------------------------------------------------------------------
    protected void touchPressedRemain(int x, int y) {
        if (isShowTableSms) {
            touchTableSms(x, y);
            doTableSms();
            return;
        }
        if (isRequestFriend || isSetupGameRule || isSetupNumberPlayer || isKickOut) {
            touchOtherSubMenu(x, y);
            return;
        }
    }

    private void touchTableSms(int x, int y) {
//        int left = leftScreen + 10;
        int top = 30;
//        left += 10;
        top += 35;
        if (top < y && y < top + 30) {
            charMoneyIndex = 0;
            return;
        }
        top += 30;
        if (top < y && y < top + 30) {
            charMoneyIndex = 1;
            return;
        }
        top += 30;
        if (top < y && y < top + 30) {
            charMoneyIndex = 2;
            return;
        }
        top += 30;
        if (top < y && y < top + 30) {
            charMoneyIndex = 2;
            return;
        }
        top += 30;
        if (top < y && y < top + 30) {
            charMoneyIndex = 3;
            return;
        }
        top += 30;
        if (top < y && y < top + 30) {
            charMoneyIndex = 4;
            return;
        }
        top += 30;
        if (top < y && y < top + 30) {
            charMoneyIndex = 5;
            return;
        }
    }

    private void touchOtherSubMenu(int x, int y) {
        if (subMenu.isTouch(x, y)) {
            if (isSetupGameRule) {
                doSetupGameRule();
                return;
            }
            if (isSetupNumberPlayer) {
                doSetupNumberPlayer();
                return;
            }
            if (isShowSubMenu) {
                doSubMenu();
                return;
            }
            if (isKickOut) {
                doKickOut();
                return;
            }
            if (isRequestFriend) {
                doFriendRequest();
                return;
            }
        }
    }

    public void doTouchReleased(int x, int y) {
    }

    public void doTouchDraged(int x, int y) {
    }

    public void processMessage(String[] part) {
        super.processMessage(part);
        if (part[0].equals("RE")) {
            gameData.setReArrData(part);
            isLoading = false;
            parent.repaint();
            return;
        }
        if (part[0].equals("SE")) {
            isLoading = false;
            gameData.setNumberPlayerInTable(part);
            gameData.diposeGameDisplay();
            showChooseTable();
            isLoading = false;
            parent.repaint();
            return;
        }
        if (part[0].equals("RO")) {
            gameData.setRoArrData(part);
            Debug.d("is show menu  :" + isShowSubMenu);
            parent.repaint();
            return;
        }
        if (part[0].equals("UUS")) {
            isWaitSetUpNumberPlayers = false;
            isLoading = false;
            gameData.setMaxPlayerInTable(part);
            parent.repaint();
            return;
        }
        if (part[0].equals("USE")) {
            gameData.setNumberPlayerInTable(part);
            return;
        }

        if (part[0].equals("SM")) {
            gameData.setTableMoneyBet(part);
            return;
        }

        if (part[0].equals("MO")) {
            gameData.setMoArrData(part);
            gameData.myMoney = gameData.playerMoneys[playerId];
            parent.repaint();
            return;
        }

        if (part[0].equals("MS")) {
            gameData.setMoneyBet(part[1]);
            isLoading = false;
            parent.repaint();
            return;
        }
        if (part[0].equals("INFO")) {
            gameData.setInfoData(part);
            return;
        }
        if (part[0].equals("GM")) {
            this.gameRule = Integer.parseInt(part[1]);
            gameData.setGameRule(gameRule);
            switchGameRule(this.gameRule);
            parent.repaint();
            return;
        }
        if (part[0].equals("CHAT")) {
            int postId = (Integer.parseInt(part[2]) - playerId + maxPlayer) % maxPlayer;
            if (postId != 0) {
                switchMessengerBox(postId, part[1]);
            }
            return;
        }
        if (part[0].equals("KICK")) {
            gameData.setKickOut(true);
            return;
        }
        if (part[0].equals("MF")) {
            if (part[2].equals(gameData.myName)) {
                dialogString = "";
                nameSend = part[1];
                questionString = nameSend + " muốn kết bạn với bạn";
                parent.repaint();
            }
            return;
        }
        if (part[0].equals("RMF")) {
            int result = Integer.parseInt(part[1]);
            if (result == 0) {
                dialogString = "Yêu cầu kết bạn không được đồng ý";
                parent.repaint();
                return;
            }
            if (result == 1) {
                dialogString = "Yêu cầu kết bạn đã được chấp nhận";
                parent.repaint();
                return;
            }
            if (result == 2) {
                dialogString = "Bạn đã kết bạn với người này";
                parent.repaint();
                return;
            }
        }
        if (part[0].equals("SC")) {
            gameData.setCardInHandData(part);
            return;
        }
        if (part[0].equals("FS")) {
            gameData.setFirstTurn(Integer.parseInt(part[1]));
            if (gamePlayType == Constants.TIEN_LEN) {
                gameData.diposeGameDisplay();
                showTienlenScene();
            } else if (gamePlayType == Constants.PHOM) {
                gameData.diposeGameDisplay();
                if (Integer.parseInt(part[1]) != gameData.getPlayerId()) {
                    showPhomScene();
                }
            }
            isLoading = false;
            return;
        }
        if (part[0].equals("BC")) {
            gameData.setBcCard(Integer.parseInt(part[1]));
            if (gamePlayType == Constants.TIEN_LEN) {
                gameData.diposeGameDisplay();
                showTienlenScene();
            } else if (gamePlayType == Constants.PHOM) {
                showPhomScene();
            }
            isLoading = false;
            return;
        }
        if (part[0].equals("START")) {
            gameData.setFirstTurn(Integer.parseInt(part[1]));
            showChessScene();
            return;
        }
        if (part[0].equals("S3C")) {
            gameData.setCardInHandData(part);
            showBacayScene();
            return;
        }
//        if (part[0].equals("RS3")) {
//            gameData.setBaCayData(part);
//            return;
//        }
        if (part[0].equals("NMS")) {
            int moneyNeeded = Integer.parseInt(part[1]);
            dialogString = "Bạn cần đặt tối thiểu " + moneyNeeded;
            isLoading = false;
            parent.repaint();
            return;
        }
        if (part[0].equals("UPAS")) {
            gameData.setTablePass(part);
            if (isWaitingPass) {
                isLoading = false;
                isWaitingPass = false;
                dialogString =
                        "Đặt mật khẩu cho bàn thành công";
                parent.repaint();
            }
            return;
        }

    }

    protected void doSubMenu() {
        switch (subMenu.getSelectedIndex()) {
            case 0:
                //nap tien
                charMoneyIndex = 0;
                isShowTableSms = true;
                isShowSubMenu = false;
                parent.repaint();
                break;

            case 1://ket ban
                isRequestFriend = true;
                showListPlayer();
                break;
            case 2:
                //loa
                showSpeaker();
                break;
            case 3:
                //kich
                isKickOut = true;
                showListPlayer();
                break;
            case 4:
                // dat cuoc
                moneyBetBox.setText("");
                isShowBetBox = true;
                isShowSubMenu = false;
                parent.repaint();
                break;
            case 5:
                //so nguoi choi
                isSetupNumberPlayer = true;
                changeSubMenu(new String[]{"4 người", "3 người", "2 người"});
                isShowSubMenu = false;
                parent.repaint();
                break;
            case 6:

                if (dialog == null) {
                    dialog = new Dialog(2, keyPad);
                }
                dialog.setString("Hãy nhập mật khẩu");
                dialog.show(true);
                parent.repaint();
                isShowSubMenu = false;
                break;
            case 7:
                // kieu choi
                changeGameRule();
                break;
        }
    }

    private void changeGameRule() {
        isSetupGameRule = true;
        if (gamePlayType == Constants.BA_CAY) {
            changeSubMenu(new String[]{"Nhất ăn tất", "Chương"});

        } else if (gamePlayType == Constants.PHOM) {
            changeSubMenu(new String[]{"Không ký gửi", "Ký gửi"});
        }
        isShowSubMenu = false;
        parent.repaint();
    }

    public void dispose() {
        positionX = null;
        positionY = null;

        nameSend = null;
//        myName = null;
        gameRuleName = null;
        saveSubMenuString = null;
        subMenuString = null;
        //------------------------------------------------------------------------
        tableHostImage = null;
        bottomBarImage = null;
        avatarImage = null;
        //  playIconImage = null;
        //-----------------------------------------------------------------------
        chatBox = null;
        moneyBetBox = null;
        messengerBox = null;
        super.dispose();
    }

    private void showListPlayer() {
        //so nguoi choi
        if (numberPlayers == 1) {
            dialogString = "Chưa có người chơi trong bàn";
            isShowSubMenu = false;
            isRequestFriend = false;
            isKickOut = false;
            parent.repaint();
            return;
        }

        // listPlayerNameIndex = 0;
        listFriendNames = new String[numberPlayers - 1];
        int count = 0;
        for (int i = 0; i < maxPlayer; i++) {
            if (gameData.isPlays[i] && i != playerId) {
                listFriendNames[count] = gameData.playerName[i];
                count++;
            }
        }
        changeSubMenu(listFriendNames);
        isShowSubMenu = false;
        parent.repaint();
    }

    protected void reponseQuestion(int result) {
        Request.responseFriendRequest(result);
    }

    public void setPlayerId(int id) {
        this.playerId = id;
    }

    public void setNumberPlayer(int number) {
        this.numberPlayers = number;
    }

    public void setTableHostId(int id) {
        this.tableHostId = id;
        setupSubMenu();
    }

    public void setNumberPlayerReady(int number) {
        numberPlayerReadys = number;
    }

    public void setMaxPlayer(int number) {
        this.maxPlayer = number;
    }

    public void setGameRule(int gameRule) {
        this.gameRule = gameRule;
        parent.repaint();
    }

    public void setFirstTurn(int turn) {
    }

    public void setMyViewId(int id) {
        myViewId = id;
        parent.repaint();
    }

    public void doKeyPound() {
        if (dialog != null) {
            if (dialog.isShow) {
                dialog.doKeyPound();
                parent.repaint();
            }
        }
    }
}
